//
//  BKAddPhotoView.m
//  lg_zhinengbeikao_english
//
//  Created by 盛超 on 2019/1/10.
//  Copyright © 2019年 zhukai. All rights reserved.
//

#import "BKAddPhotoView.h"
#import "BKPhotoView.h"
#import "HXPhotoPicker.h"

@interface BKAddPhotoView ()<BKPhotoViewDelegate>

@property (strong, nonatomic) HXPhotoManager *manager;
@property (strong, nonatomic) BKPhotoView *photoView;


@end

@implementation BKAddPhotoView

- (instancetype)init {
    if (self = [super init]) {
        
        self.editEnabled = NO;
        self.hideDeleteButton = NO;
        [self setupPhotoView];
    }
    
    return self;
}

#pragma mark - PhotoView
- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.configuration.openCamera = YES;
        _manager.configuration.photoMaxNum = 9;
        _manager.configuration.videoMaxNum = 0;
        _manager.configuration.maxNum = 9;
        
    }
    return _manager;
}

- (void)setupPhotoView {
    
    self.photoView = [[BKPhotoView alloc] initWithManager:self.manager];
    self.photoView.delegate = self;
    self.photoView.hideDeleteButton = _hideDeleteButton;
    self.photoView.editEnabled = _editEnabled;
    [self addSubview:self.photoView];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
    }];
    
    self.photoView.backgroundColor = [UIColor whiteColor];
}

- (void)setImages:(NSArray *)images {
    _images = images;
    
    if (images.count>0) {
        [self.manager addLocalImage:images selected:YES];
    }
    [self.photoView refreshView];
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(refreshPhototView:PhotoCount:)]) {
        [self.delegate refreshPhototView:self PhotoCount:images.count];
    }
}

- (void)setHideDeleteButton:(BOOL)hideDeleteButton {
    _hideDeleteButton = hideDeleteButton;
    self.photoView.hideDeleteButton = _hideDeleteButton;
}

- (void)setEditEnabled:(BOOL)editEnabled {
    _editEnabled = editEnabled;
    self.photoView.editEnabled = _editEnabled;
}

#pragma mark - BKPhotoViewDelegate
- (void)photoView:(BKPhotoView *)photoView changeComplete:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photos videos:(NSArray<HXPhotoModel *> *)videos original:(BOOL)isOriginal {
    
    NSLog(@"总图片allList=%@",allList);
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(refreshPhototView:PhotoCount:)]) {
        [self.delegate refreshPhototView:self PhotoCount:allList.count];
    }
    
}
- (void)photoView:(BKPhotoView *)photoView currentDeleteModel:(HXPhotoModel *)model currentIndex:(NSInteger)index {
    
    NSLog(@"删除图片index=%ld",index);
}

@end
